/* 
* myPageBar 1.0 
* ©2015 zhangcheng 
* Date: 2015-03-10 
* 分页条插件,根据原有的分页逻辑重新整合了js和css，使用和样式都和原有的分页条一样。
* 参数：
* currPage：当前页
* pageSize：每页几条
* ajax: { ajax请求有关参数，默认数据格式为json,可以在以后的版本中拓展其他格式,
* 		后端必须返回一个参数名为“pageCount”的字段表示总页数例如:{"pageCount":1}
* 		发送请求时将传递一个page和pageSize 表示当前页和每页条数
*			url:请求地址,
*			param:参数,例如："appId="+appId+"&startTime="+stime+"&endTime="+etime,
*			callback:回调函数,
*			ajaxStart:请求开始前调用的方法，可用于显示加载图片
*			ajaxStop:请求结束调用的方法，可用于关闭加载图片
*		}
* 方法：
* onLoad({param:param})
* 根据参数重新加载页面，可用于查询
* 
*/ 
(function($){ 
$.fn.myPageBar = function(options){ 
	var pb = {};
	pb.obj = $(this);
	pb.currPage = options.currPage;
	pb.pageSize = options.pageSize;
	pb.url = options.ajax.url;
	pb.param = options.ajax.param;
	pb.callback = options.ajax.callback;
	pb.totalPage = 0;
	if(!options.ajax.ajaxStart){
		pb.ajaxStart = function(){};
	}else{
		pb.ajaxStart = options.ajax.ajaxStart;
	}
	if(!options.ajax.ajaxStop){
		pb.ajaxStop = function(){};
	}else{
		pb.ajaxStop = options.ajax.ajaxStop;
	}
	/**
	 * 用指定参数重新加载
	 * @param p
	 */
	pb.onLoad = function(p){
		var param = "";
		if(!p.param||p.param=="")
			param = "page="+pb.currPage+"&pageSize="+pb.pageSize+"&rn="+Math.random();
		else{
			param = "page="+pb.currPage+"&pageSize="+pb.pageSize+"&rn="+Math.random()+"&"+p.param;
		}
		pb.param = p.param;
		pb.ajaxStart();
		$.ajax({
			url:pb.url+"?"+param,
			async:true,
			dataType:"json",
			error:function(xhr,msg){
				var sessionstatus = xhr.getResponseHeader("sessionstatus"); 
				console.info("sessionstatus:"+sessionstatus);
				if (sessionstatus && "timeout" == sessionstatus) {
					//alert("长时间没有操作，你需要重新登录");
					//window.location.href = $("base").attr("href")+"index.jsp";
				}else{
					alert("程序异常，请联系管理员");
				}
			},
			success:function(data){
				try{
					pb.ajaxStop();
					pb.totalPage = data.pageCount;
					pb.refreshHtml();
					pb.callback(data);
				}catch(e){
					console.error(e);
				}
			},
		});
	};
	/**
	 * 重新加载
	 */
	pb.refresh = function(){
		pb.onLoad({param:pb.param});
	};
	pb.reload = function(param){
		pb.currPage = 1;
		pb.onLoad(param);
	}
	/**
	 * 重载html
	 */
	pb.refreshHtml = function(){
		var html = "";
		if(pb.totalPage<=0){
			html="<ul class='pagination pagination-sm'>"
				+"<li class='disabled'><a class='prve'>上一页</a></li>"
				+"<li class='disabled'><a class='next'>下一页</a></li>"
				+"</ul>";
		}else{
			var startPage;
			if(pb.currPage<=3)
				startPage = 1;
			else if(pb.totalPage<=5)
				startPage = 1;
			else if(pb.totalPage-pb.currPage<2)
				startPage = pb.totalPage - 4;
			else{
				startPage = pb.currPage-2;
			}
			var endPage;
			if(pb.totalPage<5)
				endPage = pb.totalPage;
			else{
				endPage = startPage+4;
			} 
			html = "<ul class='pagination pagination-sm'>";
			if(pb.currPage<=1)
				html=html+"<li class='disabled'><a class='prve'>上一页</a></li>";
			else
				html=html+"<li><a class='prve'>上一页</a></li>";
			for(var i=startPage;i<=endPage;i++){
				if(i==pb.currPage)
					html=html+"<li class='active'><a href='javascript:void(0)'>"+i+"</a></li>";
				else
					html=html+"<li><a href='javascript:void(0)' class='toPage'>"+i+"</a></li>";
			}
			if(pb.currPage>=pb.totalPage){
				html=html+"<li class='disabled'><a class='next'>下一页</a></li>";
			}else{
				html=html+"<li><a class='next'>下一页</a></li>";
			}	
			html=html+"</ul>";
		}
		pb.obj.html(html);
		pb.obj.find(".prve").bind("click",function(){
			if(pb.currPage<=1)
				return;
			pb.currPage--;
			pb.onLoad({param:pb.param});
		});
		pb.obj.find(".next").bind("click",function(){
			if(pb.currPage>=pb.totalPage)
				return;
			pb.currPage++;
			pb.onLoad({param:pb.param});
		});
		pb.obj.find(".toPage").bind("click",function(){
			pb.currPage = parseInt($(this).html());
			pb.onLoad({param:pb.param});
		});
	};
	pb.onLoad({param:pb.param});
	return pb;
};
})(jQuery); 